package com.mp3player.model;

import android.widget.Toast;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Cuckoo322 on 3/23/2017.
 */

public class Lyric {

    private File lyricFile = null;
    private String title = null;
    private String artist = null;
    private String album = null;
    private HashMap<Long, String> lyrics = new HashMap<>();
    private List<Long> sequentialTimestamp = new ArrayList<>();
    private List<String> sequentialLyrics = new ArrayList<>();
    private String line = null;

    public Lyric(String lyricLocation) {
        lyricFile = new File(lyricLocation);
        if (isLyricFileExist()) {
            parseLyricFile();
            sortLyrics();
            for (int i = 0; i < sequentialTimestamp.size(); i++) {
                System.out.println(sequentialTimestamp.get(i) + "-->"+sequentialLyrics.get(i));
            }
        } else {
            System.out.println("lyric file is not exist...");
        }
    }

    /**
     * 将lrc文件解析成millisecond与歌词的映射
     */
    private void parseLyricFile() {
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(lyricFile)));
            while ((line = br.readLine()) != null) {
                if (line.startsWith("[ti:")) {
                    title = line.substring(4, line.length()-1);
                } else if (line.startsWith("[ar:")) {
                    artist = line.substring(4, line.length()-1);
                } else if (line.startsWith("[al:")) {
                    album = line.substring(4, line.length()-1);
                } else {
                    String reg = "\\[\\d{2}:\\d{2}.\\d{2}\\]";
                    Pattern pattern = Pattern.compile(reg);
                    Matcher matcher = pattern.matcher(line);
                    while (matcher.find()) {
                        //msg --> [##:##.##]
                        String msg = matcher.group();
                        long time = timeStr2Long(msg);
                        String[] content = pattern.split(line);
                        lyrics.put(time, content[content.length-1]);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 将歌词信息入队列
     */
    private void sortLyrics() {
        List<Long> timeStamps = new ArrayList<>();
        for (long l : lyrics.keySet()) {
            timeStamps.add(l);
        }
        Collections.sort(timeStamps);
        for (Long l : timeStamps) {
            sequentialTimestamp.add(l);
            sequentialLyrics.add(lyrics.get(l));
        }
    }

    private long timeStr2Long (String timeStr) {
        int minute = Integer.valueOf(timeStr.substring(1, 3));
        int second = Integer.valueOf(timeStr.substring(4, 6));
        int miliSecond = Integer.valueOf(timeStr.substring(7, 9));
        return (long) (minute * 60 * 1000 + second * 1000 + miliSecond * 10);
    }

    public List<Long> getSequentialTimestamp() {
        return sequentialTimestamp;
    }

    public List<String> getSequentialLyrics() {
        return sequentialLyrics;
    }

    public boolean isLyricFileExist() {
        return lyricFile.exists();
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getArtist() {
        return artist;
    }

    public void setArtist(String artist) {
        this.artist = artist;
    }

    public String getAlbum() {
        return album;
    }

    public void setAlbum(String album) {
        this.album = album;
    }

    public HashMap<Long, String> getLyrics() {
        return lyrics;
    }

    public void setLyrics(HashMap<Long, String> lyrics) {
        this.lyrics = lyrics;
    }
}
